package com.ezjie.ielts.util;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间工具类
 * 
 * @author renyangyang
 * @version 创建时间 2014年7月23日 16:16:32
 */
public class DateUtil {

	public static final SimpleDateFormat DEFAULT_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public static final SimpleDateFormat DATE_FORMAT_DATE = new SimpleDateFormat("yyyy-MM-dd");
	public static final SimpleDateFormat DEFAULT_FORMAT = new SimpleDateFormat("HH:mm");

	/**
	 * 通过时间戳 转化为 相应的 时间格式
	 * 
	 * @param timeInMillis
	 * @param dateFormat
	 * @return
	 */
	public static String getTime(long timeInMillis, SimpleDateFormat dateFormat) {
		return dateFormat.format(new Date(timeInMillis));
	}

	/**
	 * 通过时间戳 转化为 {@link #DEFAULT_DATE_FORMAT}
	 * 
	 * @param timeInMillis
	 * @return
	 */
	public static String getTime(long timeInMillis) {
		return getTime(timeInMillis, DEFAULT_DATE_FORMAT);
	}

	/**
	 * 通过时间戳 转化为 {@link #DEFAULT_DATE_FORMAT}
	 * 
	 * @param timeInMillis
	 * @return
	 */
	public static String getReplyTime() {
		long timeInMillis = System.currentTimeMillis();
		String time = getTime(timeInMillis, DEFAULT_FORMAT);
		return "今天 " + time;
	}

	/**
	 * 返回 2010-01-01 格式的日期
	 * 
	 * @return
	 */
	public static String getCurrentTime() {
		return DEFAULT_DATE_FORMAT.format(new Date());
	}

	public static String format(String date, String format1, String format2) {
		try {
			SimpleDateFormat sdf1 = new SimpleDateFormat(format1);
			Date date1 = sdf1.parse(date);
			SimpleDateFormat sdf2 = new SimpleDateFormat(format2);
			String format = sdf2.format(date1);
			return format;
		} catch (ParseException e) {
			LogUtils.d("date日期格式不正确");
			return "";
		}
	}

	/**
	 * 判断 城市的缓存时间是否有效
	 * 
	 * @param time1
	 *            缓存的时间
	 * @param time2
	 *            现在的时间
	 * @return false时间已经过期 要重新请求
	 */
	public static boolean isCityTimeExpires(String time1, String time2) {
		Date firstDate = null;
		Date secondDate = null;
		try {
			firstDate = DATE_FORMAT_DATE.parse(time1);
			secondDate = DATE_FORMAT_DATE.parse(time2);
		} catch (Exception e) {
			// 日期型字符串格式错误
		}

		int nDay = (int) ((secondDate.getTime() - firstDate.getTime()) / (24 * 60 * 60 * 1000));
		if (nDay >= 1) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * 获得当前的时间戳
	 * 
	 * @return
	 */
	public static long getCurrentTimeInLong() {
		return System.currentTimeMillis();
	}

	/**
	 * 获得当前 时间 格式为 {@link #DEFAULT_DATE_FORMAT}
	 * 
	 * @return
	 */
	public static String getCurrentTimeInString() {
		return getTime(getCurrentTimeInLong());
	}

	/**
	 * 获得当前时间 根据 相应的 格式
	 * 
	 * @param dateFormat
	 * @return
	 */
	public static String getCurrentTimeInString(SimpleDateFormat dateFormat) {
		return getTime(getCurrentTimeInLong(), dateFormat);
	}

	/**
	 * 将某种格式下的时间格式 转化为 另一种格式
	 * 
	 * 比如：2014/02/15-------> 2014-02-15
	 * 
	 * @param timeString
	 *            2014/02/15
	 * @param oldFormat
	 *            yyyy/MM/dd
	 * @param newFormat
	 *            yyyy-MM-dd
	 * @return
	 */
	public static String getNewFormatDate(String timeString, SimpleDateFormat oldFormat, SimpleDateFormat newFormat) {
		try {
			Date date = oldFormat.parse(timeString);
			return newFormat.format(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * 通过分钟获得小时 比如 90分钟 -----> 1.5小时
	 * 
	 * @param minute
	 * @return
	 */
	public static double getHourByMinute(double minute) {
		DecimalFormat dfDecimalFormat = new DecimalFormat("0.0");
		if (minute == 0) {
			return 0;
		} else if (minute > 0 && minute < 60) {
			return Double.parseDouble(dfDecimalFormat.format(minute / 60.0));
		} else if (minute == 60) {
			return 1;
		} else {
			int hour = (int) (minute / 60);
			return hour + Double.parseDouble(dfDecimalFormat.format((minute - hour * 60) / 60.0));
		}

	}

	/**
	 * 获取当前的日期
	 * 
	 * @return
	 */
	public static String getCurrentDay() {
		Calendar cal = Calendar.getInstance();
		int d = cal.get(Calendar.DATE);
		if (d < 10) {
			return "0" + d;
		} else {
			return d + "";
		}
	}

	public static String getCurrentTimeYYYYMMDD() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String format = sdf.format(new Date());
		return format;
	}

	/**
	 * 获得当前月份和年份 英文字母的
	 * 
	 * @return
	 */
	public static String getCurrentMonthAndYearEn() {
		Calendar cal = Calendar.getInstance();
		int m = cal.get(Calendar.MONTH) + 1;
		int y = cal.get(Calendar.YEAR);
		return getEnMonthSimpleStr(m) + y;
	}

	public static String getEnMonthSimpleStr(int month) {
		String mStr = "";
		// 一月 Jan. January
		// 二月 Feb. February
		// 三月 Mar. March
		// 四月 Apr. April
		// 五月 May. May
		// 六月 June. June
		// 七月 July. July
		// 八月 Aug. Auguest
		// 九月 Sept. September
		// 十月 Oct. October
		// 十一月 Nov. November
		// 十二月 Dec. December
		if (1 == month) {
			mStr = "Jan.";
		} else if (2 == month) {
			mStr = "Feb.";
		} else if (3 == month) {
			mStr = "Mar.";
		} else if (4 == month) {
			mStr = "Apr.";
		} else if (5 == month) {
			mStr = "May.";
		} else if (6 == month) {
			mStr = "June.";
		} else if (7 == month) {
			mStr = "July.";
		} else if (8 == month) {
			mStr = "Aug.";
		} else if (9 == month) {
			mStr = "Sept.";
		} else if (10 == month) {
			mStr = "Oct.";
		} else if (11 == month) {
			mStr = "Nov.";
		} else if (12 == month) {
			mStr = "Dec.";
		}
		return mStr;
	}

	/**
	 * 通过网络返回的日期获得 day
	 * 
	 * @param time
	 *            2015-03-14
	 * @return
	 */
	public static String getCurrentDayByHttp(String time) {
		try {
			Calendar cal = Calendar.getInstance();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date date = sdf.parse(time);
			cal.setTime(date);
			int d = cal.get(Calendar.DATE);
			if (d < 10) {
				return "0" + d;
			} else {
				return d + "";
			}
		} catch (Exception e) {
			LogUtils.exception(e);
			return getCurrentDay();
		}
	}

	public static String getCurrentMonthAndYearEnByHttp(String time) {
		try {
			Calendar cal = Calendar.getInstance();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date date = sdf.parse(time);
			cal.setTime(date);

			int m = cal.get(Calendar.MONTH) + 1;
			int y = cal.get(Calendar.YEAR);
			return getEnMonthSimpleStr(m) + y;
		} catch (Exception e) {
			LogUtils.exception(e);
			return getCurrentMonthAndYearEn();
		}
	}

	/**
	 * 获得当前的时区
	 * 
	 * @return
	 */
	public static String getLocalTimeZone() {
		String defaultFormat = "Z";
		SimpleDateFormat dateFormat = new SimpleDateFormat(defaultFormat);
		// +0800
		String format = dateFormat.format(new Date());
		String str = "8";
		if (format.length() >= 3) {
			String s1 = format.substring(0, 1);
			str = format.substring(1, 3);
			if ("-".equals(s1)) {
				str = s1 + str;
			}
		}
		return Integer.parseInt(str) + "";
	}

}
